package com.lwl.office.dcconverter.style;

import cn.hutool.core.util.StrUtil;
import com.lwl.office.dcconverter.cons.DCDocumentEnum;
import lombok.Data;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;

/**
 * 段落样式
 */
@Data
public class DCParagraphStyle extends AbsDCStyle{
    public DCParagraphStyle(DCDocumentEnum type) {
        super(type);
    }

    /**
     * 首行缩进：
     * html：  text-indent: 2em;
     * word:   w:ind w:leftChars="100" w:left="210"
     */
    private String ind;
    /**
     * 首行缩进的html样式
     * @return
     */
    private String indHtmlStype(){
        if(StrUtil.isNotEmpty(ind)){
            if(getType().equals(DCDocumentEnum.HTML)){
                return " text-indent: "+ind+"; ";
            }
            else if(getType().equals(DCDocumentEnum.WORD)){
                Integer num = Integer.parseInt(ind);
                return " text-indent: "+num/50+"em; ";
            }
        }
        return "";
    }

    /**
     * 对齐方式：
     * html： text-align: center;
     * word: w:jc w:val="center"
     */
    private String align;

    /**
     * 对齐方式的html样式
     * @return
     */
    private String alignHtmlType(){
        if(StrUtil.isNotEmpty(align)){
            return " text-align: "+align+"; ";
        }
        return "";
    }
    /**
     *段前距
     * html: margin-top: 20px;
     * word: w:spacing w:before="240"
     */
    private String spacingBefore;

    private String spacingBeforeHtmlType(){
        return StrUtil.isNotEmpty(spacingBefore)?
                (" margin-top: "+(getType().equals(DCDocumentEnum.HTML)?spacingBefore:(Integer.parseInt(spacingBefore)/12+"px"))+"; ")
                : "";
    }
    /**
     *断后距
     * html: margin-bottom: 20px;
     * word: w:spacing w:after="240"
     */
    private String spacingAfter;

    private String spacingAfterHtmlType(){
        return StrUtil.isNotEmpty(spacingAfter)?
                (" margin-bottom: "+(getType().equals(DCDocumentEnum.HTML)?spacingAfter:(Integer.parseInt(spacingAfter)/12+"px"))+"; ")
                : "";
    }
    /**
     * 行高
     * html: line-height: 2em;
     * word: w:spacing w:line="480
     */
    private String lineHeight;
    private String lineHeightHtmlType(){
        return StrUtil.isNotEmpty(lineHeight)?
                (" line-height: "+(getType().equals(DCDocumentEnum.HTML)?lineHeight:(Integer.parseInt(lineHeight)/240+"em"))+"; ")
                : "";
    }

    @Override
    public String toHtmlStyle() {
        StringBuilder stb = new StringBuilder();
        stb.append(" style=\"");
        stb.append(indHtmlStype());
        stb.append(alignHtmlType());
        stb.append(spacingAfterHtmlType());
        stb.append(spacingBeforeHtmlType());
        stb.append(lineHeightHtmlType());
        stb.append(" \"");
        return stb.toString();
    }

    @Override
    public void setWordStyle(Object obj) {
        XWPFParagraph paragraph = (XWPFParagraph) obj;
        CTP ctp = paragraph.getCTP();
        CTPPr pPr = ctp.isSetPPr()?ctp.getPPr():ctp.addNewPPr();
        if(StrUtil.isNotEmpty(ind)){
            CTInd ctInd = pPr.addNewInd();
            if(getType().equals(DCDocumentEnum.HTML)){
                int inum = Integer.parseInt(ind.replace("em",""));
                ctInd.setLeftChars(BigInteger.valueOf(50*inum));
                ctInd.setLeft(105*inum);
            }
        }
        if(StrUtil.isNotEmpty(align)){
            CTJc ctJc = pPr.addNewJc();
            ctJc.setVal(align.equals("center")? STJc.CENTER:(align.equals("right")?STJc.RIGHT: STJc.LEFT));
        }
        if(StrUtil.isNotEmpty(spacingBefore)){
            if(getType().equals(DCDocumentEnum.HTML)){
                int bef = Integer.parseInt(spacingBefore.replace("px",""));
                CTSpacing ctSpacing = pPr.isSetSpacing() ? pPr.getSpacing() : pPr.addNewSpacing();
                ctSpacing.setBefore(bef*12);
            }
        }
        if(StrUtil.isNotEmpty(spacingAfter)){
            if(getType().equals(DCDocumentEnum.HTML)){
                int aft = Integer.parseInt(spacingAfter.replace("px",""));
                CTSpacing ctSpacing = pPr.isSetSpacing() ? pPr.getSpacing() : pPr.addNewSpacing();
                ctSpacing.setAfter(aft*12);
            }
        }
        if(StrUtil.isNotEmpty(lineHeight)){
            if(getType().equals(DCDocumentEnum.HTML)){
                int line = Integer.parseInt(lineHeight.replace("em",""));
                CTSpacing ctSpacing = pPr.isSetSpacing() ? pPr.getSpacing() : pPr.addNewSpacing();
                ctSpacing.setLine(line*120);
            }
        }
    }
}
